// pages/my/edit/gerenxinxi/gerenxinxi.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:{},
    user: {},
    imgUrl: '',
    fileList: [],
    userInfo:{}
  },
  //增加到数据库中
  edit(e) {
    //console.log(wx.getStorageSync('userInfo').userId,)
    //console.log(e)
    // console.log(this.data.imgUrl)
    // console.log(this.data.date.userName)
    console.log(wx.getStorageSync('userInfo'))
    this.setData({
      user:{
        userId:this.data.date.userId,
        userName:this.data.date.userName,
        avatar:this.data.imgUrl,
        nickName:e.detail.value.xingming,
        description:e.detail.value.zuoyouming,
      }
    })
    wx.request({
      url: 'http://localhost:8080/system/user',
      method:"put",
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data:JSON.stringify(this.data.user),
      success:(resp)=>{
        console.log(resp)
        if(resp.data.code == 200){
         let userInfo =  wx.getStorageSync('userInfo')
         userInfo.avatar = this.data.imgUrl
          userInfo.nickName = e.detail.value.xingming
          // userInfo.nickName = "e.detail.value.xingming"
          //console.log(userInfo.nickName,"2sdehuigruty")
          userInfo.description = e.detail.value.zuoyouming
          wx.setStorageSync('userInfo', userInfo)
          wx.showToast({
            title: '修改成功,请重新登录',
            icon:'success',
            duration: 5000
          })
          setTimeout(()=>{
            wx.navigateTo({
              url: '/pages/my/login/login',
            })
          },1000)
        }
        console.log(wx.getStorageSync('userInfo'),12834786574)
      }
    })
  },
  //  图片上传成功回调
  handleSuccess(e) {
    const {
      files
    } = e.detail;
    this.setData({
      fileList: files,
    });
  },
  // 图片函数回调
  handleAdd(e) {
    const {
      fileList
    } = this.data;
    const {
      files
    } = e.detail;
    // 方法1：选择完所有图片之后，统一上传，因此选择完就直接展示
    files.forEach(file => {
      this.onUpload(file)
    });
    console.log(this.data.fileList)
  },
  onUpload(file) {
    const task = wx.uploadFile({
      url: 'http://localhost:8080/common/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      success: (resp) => {
        this.setData({
          imgUrl: JSON.parse(resp.data).msg
        })
      },
    })
  },
  handleRemove(e) {
    const {
      index
    } = e.detail;
    const {
      fileList
    } = this.data;
    fileList.splice(index, 1);
    this.setData({
      fileList,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("你好")
    // console.log(options.date)
    // console.log("four")
    this.setData({
      date:JSON.parse(options.date)
    })
    console.log("three")
    //console.log(JSON.parse(options.date))
    console.log(this.data.date)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})